package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Car;
import com.chixing.entity.Customer;
import com.chixing.entity.Img;
import com.chixing.entity.Orders;
import com.chixing.entity.vo.OrderVO;
import com.chixing.mapper.CarMapper;
import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.ImgMapper;
import com.chixing.mapper.OrdersMapper;
import com.chixing.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerResponse;

import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;


@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ImgMapper imgMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CarMapper carMapper;

    @Override
    public Result createOrder(Orders order) {
        int rows = ordersMapper.insert(order);
        if (rows > 0)
            return Result.success("添加订单成功",order);
        else
            return Result.fail("添加订单失败",null);
    }

    @Override
    public Result getById(Integer orderId) {
        Orders order = ordersMapper.selectById(orderId);
        if (order != null)
            return Result.success("查询成功",order);
        else
            return Result.fail("查询失败",null);
    }

    @Override
    public Result getOrderAll() {
        List<Orders> ordersList = ordersMapper.selectList(null);
        if (ordersList != null)
            return Result.success("查询所有订单成功",ordersList);
        else
            return Result.fail("查询所有订单失败",null);
    }

    @Override
    public Result updateOrderStatus(Orders order,Integer orderStatusId) {
        order.setOrderStatus(orderStatusId);
        if (order.getOrderStatus().equals(orderStatusId))
            return Result.success("update订单状态成功",orderStatusId);
        else
            return Result.fail("update订单状态失败",null);
    }

    @Override
    public Result deleteOrderById(Integer orderId) {
        int rows = ordersMapper.deleteById(orderId);
        if (rows > 0)
            return Result.success("删除订单成功",orderId);
        else
            return Result.fail("删除订单失败",null);
    }


    @Override
    public Result getOrdersByCustomerId(Integer customerId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_id",customerId);
        List<OrderVO> orderVoList = new ArrayList<>();
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        System.out.println("查询到的我的订单是：" +orders);
        for(Orders o :orders){
            OrderVO orderVO = new OrderVO();
            orderVO.setOrderId(o.getOrderId());
            orderVO.setOrderNo(o.getOrderNo());
            orderVO.setOrderDropoffTime(o.getOrderDropoffTime());
            orderVO.setOrderPickupTime(o.getOrderPickupTime());
            orderVO.setOrderStatus(o.getOrderStatus());
//         orderVO.setOrderDay(o.getOrderDropoffTime().minusDays(o.getOrderPickupTime()));
            Car car = carMapper.selectById(o.getCarId());
            orderVO.setCarName(car.getCarName());
            orderVO.setCarCase(car.getCarCase());
            orderVO.setCarSeat(car.getCarSeat());
            orderVO.setCarDisplacement(car.getCarDisplacement());
            orderVoList.add(orderVO);

        }
        if (orders != null)
            return Result.success("查询用户所有订单成功",orderVoList);
        else
            return Result.fail("查询用户所有订单失败",null);
    }



    // 0 : 待支付  1： 已完成  2： 已取消
    @Override
    public Result getOrdersByCustomerIdAndStatus(Integer customerId,int status){
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_id",customerId);
        queryWrapper.eq("order_status",status);
        List<OrderVO> orderVoList = new ArrayList<>();
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        System.out.println(">>>>>>>>>>>查询到的我的订单是状态为"+ status +"：" +orders);

        for(Orders o :orders){
            OrderVO orderVO = new OrderVO();
            orderVO.setOrderId(o.getOrderId());
            orderVO.setOrderNo(o.getOrderNo());
            orderVO.setOrderDropoffTime(o.getOrderDropoffTime());
            orderVO.setOrderPickupTime(o.getOrderPickupTime());
            orderVO.setOrderStatus(o.getOrderStatus());
//         orderVO.setOrderDay(o.getOrderDropoffTime().minusDays(o.getOrderPickupTime()));
            Car car = carMapper.selectById(o.getCarId());
            orderVO.setCarName(car.getCarName());
            orderVO.setCarCase(car.getCarCase());
            orderVO.setCarSeat(car.getCarSeat());
            orderVO.setCarDisplacement(car.getCarDisplacement());
            orderVoList.add(orderVO);

        }
        if (orders != null)
            return Result.success("查询用户所有订单成功",orderVoList);
        else
            return Result.fail("查询用户所有订单失败",null);
    }
    @Override
    public Result getOrderByOrderNum(String orderNum) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNum);
        List<OrderVO> orderVoList = new ArrayList<>();

        List<Orders> orders = ordersMapper.selectList(queryWrapper);

        for(Orders o :orders){
            OrderVO orderVO = new OrderVO();

            orderVO.setOrderId(o.getOrderId());
            orderVO.setOrderNo(o.getOrderNo());
            orderVO.setOrderStatus(o.getOrderStatus());
            orderVO.setOrderPickupShop(o.getOrderPickupShop());
            orderVO.setOrderDropoffShop(o.getOrderDropoffShop());

            orderVO.setOrderPickupAddress(o.getOrderPickupAddress());
            orderVO.setOrderDropoffAddress(o.getOrderDropoffAddress());
            orderVO.setOrderPickupTime(o.getOrderPickupTime());
            orderVO.setOrderDropoffTime(o.getOrderDropoffTime());
            long days= ChronoUnit.DAYS.between(o.getOrderPickupTime(),o.getOrderDropoffTime());
            orderVO.setOrderlease(days);
            Car car = carMapper.selectById(o.getCarId());
            orderVO.setCarName(car.getCarName());
            orderVO.setCarCase(car.getCarCase());
            orderVO.setCarSeat(car.getCarSeat());
            orderVO.setCarDisplacement(car.getCarDisplacement());
            orderVO.setCarPrice(car.getCarPrice());
            orderVO.setCarImgId(car.getCarImgId());
            Img img =imgMapper.selectById(car.getCarImgId());
            orderVO.setImgPath(img.getImgPath());

            Customer cust = customerMapper.selectById(o.getCustId());
            orderVO.setCustName(cust.getCustName());

            orderVoList.add(orderVO);

        }
        if (orders != null){

            return Result.success("查询该订单成功",orderVoList);
        }

        else{
            return Result.fail("查询该订单失败",null);
        }


    }


}
